package com.jdew.thkj.view;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.alibaba.fastjson.JSONObject;
import com.jdew.thkj.R;
import com.jdew.thkj.activity.RegisterActivity;
import com.jdew.thkj.adapter.ListViewAdapter;
import com.jdew.thkj.bean.CountyAreaBean;
import com.jdew.thkj.manager.AppConfig;
import com.jdew.thkj.network.RetrofitManager;
import com.jdew.thkj.network.api.RequestApi;
import com.jdew.thkj.onitemInterface.SearchClickListener;
import com.jdew.thkj.utils.MD5Utils;
import com.jdew.thkj.utils.RegexValidateUtils;
import com.jdew.thkj.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yangyang on 2017/9/12.
 */

public class PopupWindowView extends PopupWindow {
    ListView listView;
    private int popupWidth;
    private int popupHeight;
    private View parentView;
    private ListViewAdapter adapter;
    public List<CountyAreaBean.DataBean> list = new ArrayList<>();
    private SearchClickListener onitemcallBack;
    public SharedPreferences sp;
    public SharedPreferences.Editor editor;
    public PopupWindowView(Context context) {
        super(context);
        initView(context);
        setPopConfig();
    }
   public void setOnItemClickListener(SearchClickListener onitemcallBack){
       this.onitemcallBack = onitemcallBack;
   }
    /**
     * 初始化控件
     *
     * @param context
     */
    private void initView(final Context context) {
        parentView = View.inflate(context, R.layout.pop_menu, null);
        setContentView(parentView);
        sp = context.getSharedPreferences("user", Activity.MODE_PRIVATE);
        editor = sp.edit();
        listView=parentView.findViewById(R.id.listView);
        adapter=new ListViewAdapter(context,list);
        listView.setAdapter(adapter);
        GetGlobalRoaming();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (null != onitemcallBack) {
                    onitemcallBack.onItemClick(list.get(position).getCountryID());
                }
            }
        });
    }

    /**
     * 配置弹出框属性
     *
     * @version 1.0
     * @createTime 2015/12/1,12:45
     * @updateTime 2015/12/1,12:45
     * @createAuthor
     * @updateAuthor
     * @updateInfo (此处输入修改内容, 若无修改可不写.)
     */
    private void setPopConfig() {
//        this.setContentView(mDataView);//设置要显示的视图
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        ColorDrawable dw = new ColorDrawable(0xcceeeeee);
        this.setBackgroundDrawable(dw);
        this.setOutsideTouchable(true);// 设置外部触摸会关闭窗口
        //获取自身的长宽高
        parentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        popupHeight = parentView.getMeasuredHeight();
        popupWidth = parentView.getMeasuredWidth();
    }

    public void GetGlobalRoaming() {
        String timesamp = RegexValidateUtils.getTime13();
        String numsamp = RegexValidateUtils.getRandom10();
        RetrofitManager.getInstance(AppConfig.BASE_URL)
                .createReq(RequestApi.class).GetGlobalRoaming(RegexValidateUtils.getUniquePsuedoID(), timesamp, numsamp, MD5Utils.getMD5(RegexValidateUtils.ASCIISort(timesamp + numsamp + RegexValidateUtils.getUniquePsuedoID() + sp.getString("SignToken", ""))).toUpperCase()).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<CountyAreaBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onNext(CountyAreaBean bean) {
                if (bean.getCode().equals("0")) {
                    list.clear();
                    list.addAll(bean.getData());
                    adapter.setList(list);
                }
            }
        });
    }
}
